#include "e2d/ui/HierarchyEditor.h"
#include "e2d/imgui/Editor.h"

using namespace e2d;

e2d::HierarchyEditor::HierarchyEditor()
	:fxcc::imgui::Layer("HierarchyEditor")
{


};

void e2d::HierarchyEditor::RefreshY()
{
	m_RefreshScroolY = true;
}

void e2d::HierarchyEditor::ViewFunc()
{
	auto m_Inst = m_Scene->m_EditingEntity.m_Entity;

	if (!m_Inst)
	{
		return;
	}
	Edit();
};

void e2d::HierarchyEditor::HandleFunc()
{
	auto m_Inst = m_Scene->m_EditingEntity.m_Entity;

	auto& sg = e2d::State::Inst();

	if (sg.hireEntityFlag)
	{
		//m_Inst = sg.selectedEntity;
		RefreshY();
		sg.hireEntityFlag = false;
	}

	if (sg.entityAddComponent)
	{
		ztclog::info("add component");
		e2d::Components::AddComponent(m_Inst, m_Inst->m_NewComponentType);
		sg.entityAddComponent = false;
	}
}

void e2d::HierarchyEditor::Edit()
{
	auto m_Inst = m_Scene->m_EditingEntity.m_Entity;

	auto& sg = e2d::State::Inst();

	int index = 8;

	if (m_Editor.TreeNode("Info", m_Inst->m_ImguiFlag, index++))
	{
		m_Editor.EditInst<e2d::Object>("object", *m_Inst);
		m_Editor.EditInst("Name", m_Inst->m_Name);
		m_Editor.EditInst("UUID", m_Inst->m_UUID);

		m_Editor.TreePop();
	};
	
	m_FinalEditor.TryTreeEditEntity(m_Inst);

	if (m_Editor.TreeNode("Tools", m_Inst->m_ImguiFlag, index++))
	{
		auto& type = m_Inst->m_NewComponentType;

		if (ImGui::BeginCombo("Type", m_Inst->m_NewComponentType.c_str()))
		{

#ifdef E2D_BUILD_TRANS
			ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 1.0f, 0.5f, 1.0f));
			ImGui::Text("Trans");
			ImGui::PopStyleColor();
			ImGui::Separator();
			ComboLabels(e2d::trans::Components::m_Labels, m_Inst->m_NewComponentType);
#endif
#ifdef E2D_BUILD_RENDER
			ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 1.0f, 0.5f, 1.0f));
			ImGui::Text("Render");
			ImGui::PopStyleColor();
			ImGui::Separator();
			ComboLabels(e2d::render::Components::m_Labels, m_Inst->m_NewComponentType);
#endif
#ifdef E2D_BUILD_AUDIO
			ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 1.0f, 0.5f, 1.0f));
			ImGui::Text("Audio");
			ImGui::PopStyleColor();
			ImGui::Separator();
			ComboLabels(e2d::audio::Components::m_Labels, m_Inst->m_NewComponentType);
#endif
#ifdef E2D_BUILD_PHYSICS
			ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 1.0f, 0.5f, 1.0f));
			ImGui::Text("physics");
			ImGui::PopStyleColor();
			ImGui::Separator();
			ComboLabels(e2d::physics::Components::m_Labels, m_Inst->m_NewComponentType);
#endif
#ifdef E2D_BUILD_FUI
			ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 1.0f, 0.5f, 1.0f));
			ImGui::Text("FUI");
			ImGui::PopStyleColor();
			ImGui::Separator();
			ComboLabels(e2d::fui::Components::m_Labels, m_Inst->m_NewComponentType);
#endif
			ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 1.0f, 0.5f, 1.0f));
			ImGui::Text("Impl");
			ImGui::PopStyleColor();			ImGui::Separator();
			ComboLabels(e2d::impl::Components::m_Labels, m_Inst->m_NewComponentType);

			ImGui::EndCombo();
		};

		if (ImGui::Button("Submit"))
		{
			sg.entityAddComponent = true;

		}
		m_Editor.TreePop();
	}


	if (m_RefreshScroolY)
	{
		ImGui::SetScrollY(m_Inst->m_ScrollY);
		m_RefreshScroolY = false;
	}
	m_Inst->m_ScrollY = ImGui::GetScrollY();


	if (ImGui::BeginPopupContextWindow())
	{
		if (ImGui::BeginMenu("New"))
		{
			//if (ImGui::MenuItem("ImageQuad"))
			//{
			//	auto m_Inst = m_Scene->m_EditingEntity.m_Entity;

			//	this->m_Inst->GetOrEmplace<e2d::ImageQuad>();
			//}

			ImGui::EndMenu();
		}

		ImGui::EndPopup(); 
	}
}

bool e2d::HierarchyEditor::ComboLabels(const std::vector<std::string>& labels, std::string& target)
{
	bool flag{ 0 };
	for (const auto& label : labels)
	{
		flag = ImGui::Selectable(label.c_str(), label == target);

		if (flag)
		{
			target = label;
		}
	}
	return flag;
}

